<template>
  <ContentWrap>
    <avue-crud
      ref="crudRef"
      v-model="tableForm"
      v-model:page="tablePage"
      v-model:search="tableSearch"
      :table-loading="loading"
      :data="tableData"
      :option="tableOption"
      @refresh-change="getTableData"
      @size-change="sizeChange"
      @current-change="currentChange"
    >
      <template #menu-left="{ size }">
        <el-button
          type="primary"
          plain
          :size="size"
          @click="importDialog = true"
          :loading="exportLoading"
          v-hasPermi="['jeelowcode:i18n:import']"
        >
          <Icon icon="clarity:import-line"></Icon>
          <span>导入国际化配置</span>
        </el-button>
        <el-button
          type="success"
          plain
          :size="size"
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['jeelowcode:i18n:export']"
        >
          <Icon icon="clarity:export-line"></Icon>
          <span>导出国际化配置</span>
        </el-button>
      </template>
      <template #fileName="{ row }">
        <div
          class="cursor-pointer hover:c-#409EFF"
          @click="downloadByUrl({ url: row.fileUrl, fileName: row.fileName })"
        >
          {{ row.fileName }}
        </div>
      </template>
    </avue-crud>
    <!-- 导入 -->
    <DesignPopup
      v-model="importDialog"
      title="导入国际化配置"
      width="900px"
      :dialog-params="{ alignCenter: true }"
      :handleClose="importRef?.handleClose"
    >
      <template #default="{ isFull }">
        <ImportI18n
          ref="importRef"
          :show="importDialog"
          :isFull="isFull"
          :locale-map="localeStore.localeMap"
          @close-popup="importDialog = false"
          @reset-change="resetChange"
        ></ImportI18n>
      </template>
    </DesignPopup>
  </ContentWrap>
</template>
<script lang="ts" setup>
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { downloadByUrl } from '@/utils/filt'
import * as I18nApi from '@/api/design/i18n'
import { CommonStatusEnum } from '@/utils/constants'
import ImportI18n from './ImportI18n.vue'
import { useLocaleStoreWithOut } from '@/store/modules/locale'

defineOptions({ name: 'SystemPost' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const { getCurrPermi } = useCrudPermi()
const localeStore = useLocaleStoreWithOut()

const loading = ref(true) // 列表的加载中
const tableOption = reactive({
  align: 'center',
  headerAlign: 'center',
  addBtn: false,
  menu: false,
  column: {
    fileName: {
      label: '导入文件'
    },
    userName: {
      label: '操作人',
      dicData: getIntDictOptions(DICT_TYPE.COMMON_STATUS),
      rules: [{ required: true, message: '状态不能为空', trigger: 'blur' }],
      value: CommonStatusEnum.ENABLE
    },
    createTime: {
      label: '导入时间',
      display: false,
      type: 'datetime',
      startPlaceholder: '开始时间',
      endPlaceholder: '结束时间',
      formatter: (row, val, value, column) => {
        return dateFormatter(row, column, val)
      }
    }
  }
}) //表格配置
const tableForm = ref<{ id?: number }>({})
const tableData = ref([])
const tableSearch = ref({})
const tablePage = ref({
  currentPage: 1,
  pageSize: 10,
  total: 0
})
const exportLoading = ref(false) // 导出的加载中
const crudRef = ref()

const importDialog = ref(false)
const importRef = ref()

useCrudHeight(crudRef)

/** 查询列表 */
const getTableData = async () => {
  loading.value = true
  let searchObj = {
    pageNo: tablePage.value.currentPage,
    pageSize: tablePage.value.pageSize
  }
  try {
    const data = await I18nApi.getImportList(searchObj)
    tableData.value = data.records
    tablePage.value.total = data.total
  } finally {
    loading.value = false
  }
}

const resetChange = () => {
  tablePage.value.currentPage = 1
  getTableData()
}

const sizeChange = (pageSize) => {
  tablePage.value.pageSize = pageSize
  resetChange()
}

const currentChange = (currentPage) => {
  tablePage.value.currentPage = currentPage
  getTableData()
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await I18nApi.exportExcelData({ param: localeStore.localeMap })
    download.excel(data, '国际化配置.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(async () => {
  await getTableData()
})
</script>
